<script setup lang="ts">
import { ref, watch } from 'vue';
import type { FormInst, FormRules } from 'naive-ui';
import { useDialog } from 'naive-ui';
import { localStg } from '@/utils/storage';
import { insertBidFolderAction, msgCheckAction } from '@/service/api';
import { successMessage } from '@/utils/utils';

const showFolderModal = ref<boolean>(false);

interface Props {
  folderData?: any[];
  parentId?: number;
}

const props = withDefaults(defineProps<Props>(), {
  folderData: () => [],
  parentId: 0
});

const formRef = ref<HTMLElement & FormInst>();

interface FormData {
  parentId: number;
  folderName: string;
  status: number;
}

const formData = ref<FormData>({
  parentId: 0, // 父级id
  folderName: '', // 文件名称
  status: 0 // 终端（1：机顶盒 2：小程序）
});

// 校验规则
const rules: FormRules = {
  folderName: {
    required: true,
    message: '请输入',
    trigger: 'blur'
  }
};

watch(
  () => props.parentId,
  value => {
    if (value) {
      formData.value.parentId = value;
    }
  },
  { immediate: true }
);

const emit = defineEmits(['bidLibraryQuery', 'refresh']);

function handleJumpFolder(item: any): any {
  emit('bidLibraryQuery', 4, item);
}

// 新建文件夹
function buildNewFolder(): any {
  formData.value.folderName = '';
  showFolderModal.value = true;
}

const dialog = useDialog();

// 新增文件夹提交
async function handleSubmit() {
  await formRef.value?.validate(async errors => {
    if (!errors) {
      const checkParams: any = {
        moduleValue: localStg.get('moduleId') || 1,
        keyword: formData.value.folderName
      };

      const { data: checkData, error: checkError } = await msgCheckAction(checkParams);
      if (!checkError) {
        if (checkData && checkData.length > 0) {
          dialog.error({
            title: '温馨提示',
            content: `公司名称中包含以下敏感词：\n${checkData.join('，')}`,
            style: 'white-space: pre-line;',
            positiveText: '确认'
          });
          return;
        }
      }

      const { data, error } = await insertBidFolderAction(formData.value);
      if (!error) {
        showFolderModal.value = false;
        successMessage('新增成功');
        emit('refresh', props.parentId);
      }
    }
  });
}
</script>

<template>
  <div class="w-full">
    <div class="files">
      <div class="uploadFile file">
        <img class="typeImg" src="@/assets/bid-materials/folder-trans.png" alt="" />
        <div class="fileName">
          <NButton
            color="#FFFFFF"
            text-color="#333333"
            strong
            style="width: 220px; font-size: 16px"
            @click="buildNewFolder"
          >
            新建文件夹
          </NButton>
        </div>
      </div>
      <div v-for="item in folderData" :key="item.id" class="file" @click="handleJumpFolder(item)">
        <img class="typeImg" src="@/assets/bid-materials/folder.png" alt="" />
        <div class="fileName">{{ item.folderName }}</div>
      </div>
    </div>
    <NModal v-model:show="showFolderModal">
      <NCard
        header-style="font-size: 32px;colo: '#333333'"
        footer-style="text-align:center"
        style="width: 1080px; border-radius: 30px"
        title="新建文件夹"
        :bordered="false"
        size="huge"
        role="dialog"
        aria-modal="true"
        closable
        @close="
          () => {
            showFolderModal = false;
          }
        "
      >
        <template #header-extra></template>
        <span style="color: #4501b7; font-size: 20px">适用于公司内不同类型的标书知识库搭建</span>
        <NForm
          ref="formRef"
          label-placement="left"
          label-width="100"
          size="large"
          style="margin-top: 30px"
          :model="formData"
          :rules="rules"
        >
          <NFormItem label="文件名称" required path="folderName">
            <NInput v-model:value="formData.folderName" placeholder="请输入" />
          </NFormItem>
        </NForm>
        <template #footer>
          <NButton secondary type="primary" size="large" @click="handleSubmit">+ 新建</NButton>
        </template>
      </NCard>
    </NModal>
  </div>
</template>

<style scoped lang="scss">
.title {
  font-weight: 500;
  font-size: 22px;
  color: #333333;
  line-height: 48px;
  padding-left: 32px;
  margin-top: 10px;
  border-bottom: #ebeef5 1px solid;
}
.files {
  padding-top: 20px;
  width: 1583px;
  height: 650px;
  display: flex;
  flex-wrap: wrap;
  .file {
    width: 281px;
    height: 180px;
    border-radius: 12px;
    border: 1px solid #ebeef5;
    margin: 16px 16px;
    position: relative;
    &:hover {
      background: #f3f4f8;
      cursor: pointer;
    }
    .typeImg {
      position: absolute;
      top: 44px;
      left: 106px;
      width: 70px;
      height: 59px;
    }
    .fileName {
      text-align: center;
      width: 281px;
      padding: 0 30px;
      margin-top: 120px;
      font-weight: 400;
      font-size: 18px;
      color: #333333;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      line-clamp: 1; /* 这里的3表示最多显示2行 */
      overflow: hidden;
    }
  }
}
.uploadFile {
  background: #f3f4f8;
}
</style>
